package com.feng.zhihao.apigeteway.util;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.lang3.ArrayUtils;

import com.feng.zhihao.apigeteway.config.spring.DAO;

public class DBUtil {
	public static List<Map<String, Object>> get(String sql,Object... params) throws SQLException{
		Connection conn = null;
		try {
			conn = DAO.dataSource.getConnection();
			QueryRunner qr = new QueryRunner();
			List<Map<String, Object>> al = (List<Map<String, Object>>)qr.query(conn,sql, new MapListHandler(),params);
			return al;
		}finally {
			if(conn != null){
				conn.close();
			}
		}
	}
	 /** 
     *  
     * @param <T>分页查询 
     * @param beanClass 
     * @param sql 
     * @param page 
     * @param count 
     * @param params 
     * @return 
	 * @throws SQLException 
     */  
    public static List<Map<String, Object>> page(String sql, int page, int pageSize, Object... params) throws SQLException {  
        if (page <= 1) {  
            page = 0;  
        }  
        return get(sql + " LIMIT ?,?", ArrayUtils.addAll(params, new Object[] { page, pageSize }));  
    }
}
